package com.hmdp.config;

import cn.hutool.json.JSONUtil;
import com.hmdp.entity.VoucherOrder;
import com.hmdp.service.impl.VoucherOrderServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


@Slf4j
@Configuration
public class RabbitConfig {
    public static final String QUEUE = "seckill.Queue";
    public static final String EXCHANGE = "seckill.Exchange";
    public static final String ROUTINGKEY = "seckill.#";

    @Bean
    public MessageConverter jacksonMessageConvertor(){
        return new Jackson2JsonMessageConverter();
    }
//    @Bean
//    public Queue queue(){
//        return new Queue(QUEUE);
//    }
//    @Bean
//    public TopicExchange topicExchange(){
//        return new TopicExchange(EXCHANGE);
//    }
//    @Bean
//    public Binding binding(){
//        return BindingBuilder.bind(queue()).to(topicExchange()).with(ROUTINGKEY);
//    }

    @Autowired
    private VoucherOrderServiceImpl voucherOrderService;
    //使用注解声明，不存在的会自动创建
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = QUEUE),
            exchange = @Exchange(name = EXCHANGE, type = ExchangeTypes.TOPIC),
            key = ROUTINGKEY
    ))
    public void listenTopicQueue1(String msg) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        log.info("消费者1接收到消息: "+msg);
        VoucherOrder voucherOrder = JSONUtil.toBean(msg, VoucherOrder.class);
        Class<?> voucherOrderServiceClass = voucherOrderService.getClass();
        Method createVoucherOrderMethod = voucherOrderServiceClass.getDeclaredMethod("createVoucherOrder", VoucherOrder.class);
        //这里为什么设置访问权限还是访问不了的原因在于，这个类被spring增强了，类名都不一样com.hmdp.service.impl.VoucherOrderServiceImpl$$EnhancerBySpringCGLIB$$bd4f1b3.createVoucherOrder(com.hmdp.entity.VoucherOrder)
        createVoucherOrderMethod.setAccessible(true);
        createVoucherOrderMethod.invoke(voucherOrderService, voucherOrder);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = QUEUE),
            exchange = @Exchange(name = EXCHANGE, type = ExchangeTypes.TOPIC),
            key = ROUTINGKEY
    ))
    public void listenTopicQueue2(String msg) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        log.info("消费者2接收到消息: "+msg);
        VoucherOrder voucherOrder = JSONUtil.toBean(msg, VoucherOrder.class);
        Class<?> voucherOrderServiceClass = voucherOrderService.getClass();
        Method createVoucherOrderMethod = voucherOrderServiceClass.getDeclaredMethod("createVoucherOrder", VoucherOrder.class);
        //这里为什么设置访问权限还是访问不了的原因在于，这个类被spring增强了，类名都不一样com.hmdp.service.impl.VoucherOrderServiceImpl$$EnhancerBySpringCGLIB$$bd4f1b3.createVoucherOrder(com.hmdp.entity.VoucherOrder)
        createVoucherOrderMethod.setAccessible(true);
        createVoucherOrderMethod.invoke(voucherOrderService, voucherOrder);
    }
}
